#include <iostream>
using namespace std;

class Person 
{

    public:
       Person(){
         cout<< "Person()"<<endl;
       }
      virtual ~Person(){
         cout<< "~Person()"<<endl;
       }
      virtual void eat() = 0;
};

class Student: public Person
{

    public:
       Student(){
        cout<< "Student()"<<endl;
       }
       virtual ~Student(){
        cout << "~Student()"<<endl;
       }
       virtual void eat(){
           cout <<"Student()::eat()"<<endl;
       }
       void study()
       {
           cout << "Student::study()" << endl;
       }  
};
class Teacher: public Person
{

    public:
       Teacher(){
        cout<< "Teacher()"<<endl;
       }
       virtual ~Teacher(){
        cout << "~Teacher()"<<endl;
       }
       virtual void eat(){
           cout <<"Teacher()::eat()"<<endl;
       }
       void teach()
       {
           cout << "Teacher::teach()" << endl;
       }  
};
void doSomething(Person *p)
{
    p->eat();
    if(typeid(*p) == typeid(Student)) {
        Student *stu = dynamic_cast<Student*>(p);
        stu->study();
    }
    if(typeid(*p) == typeid(Teacher)) {
        Teacher *tea = dynamic_cast<Teacher*>(p);
        tea->teach();
    }
}
int main(){
    Student stu;
    Teacher tea;
    //doSomething在运行时使用 typeid 识别类型、使用 dynamic_cast 进行类型转换。
    doSomething(&stu);
    doSomething(&tea);
    return 0;
    
}